import base64
import json
import sys
import datetime
import requests
import urllib
import time
import hashlib
import aes



# M2017051513600452200|1234567890123456|6543210987654321|卡号|姓名|身份证|电话|金额|类型
def pay_core(url, data_obj):
    data = json.dumps(data_obj)
    print(data)
    cipher = aes.AesEncryption(aeskey)
    data = cipher.encrypt(data)
    data = base64.urlsafe_b64encode(data).decode('utf-8')
    request_obj = {
        'merchantid': merchantid,
        'data': data,
        'timestamp': str(int(time.time()))
    }
    sign_string = secretkey + merchantid + data + request_obj['timestamp'] + secretkey
    sign = hashlib.sha256(sign_string.encode("utf-8")).hexdigest()
    request_obj['sign'] = sign
    body = urllib.parse.urlencode(request_obj)
    print(body)
    c = input("confirm c contune ,e exit")
    if c != 'c':
        exit()
    response = requests.post(url, data=request_obj)
    print(response.content)
    rsp_data = json.loads(response.content.decode('utf-8'))
    print(response.content)
    if rsp_data['message'] != 'success':
        print(response.content)
        return
    rsp_sign_string = secretkey + rsp_data['merchantid'] + rsp_data['data'] + rsp_data['timestamp'] + secretkey
    rsp_sign = hashlib.sha256(rsp_sign_string.encode('utf-8')).hexdigest()
    if rsp_sign != rsp_data['sign']:
        print("sign error")
    data = base64.urlsafe_b64decode(rsp_data['data'].encode('utf-8'))
    result = cipher.decrypt(data)
    print(result)


if __name__ == '__main__':
    merchantid = "M2017071113922991567"
    secretkey = "F839220E463C03DE93C1CFC2F16DC370"
    aeskey = "3B65571F4EB0F92E"

    channel = sys.argv[1]
    if channel == 'query':
        data_obj = {
            "ordernumber" : sys.argv[2]
        }
        url = 'https://gw.3035pay.com/pay/unionpay/query/entrust'
        print(json.dumps(data_obj))
        pay_core(url, data_obj)
        exit()
    elif channel == 'qq':
        data_obj = {
            "ordernumber" : sys.argv[2]
        }
        url = 'https://api.3035pay.com/pay/unionpay/query/quick'
        print(json.dumps(data_obj))
        pay_core(url, data_obj)
        exit()



    now = datetime.datetime.now()
    orderdate = now.strftime('%Y%m%d%H%M%S')
    channel = sys.argv[1]
    if channel == 'cc':
        url = 'https://gw.3035pay.com/pay/unionpay/quick/credit'
        data_obj = {
            "ordernumber": "wk" + orderdate,
            "amount": "15000",
            "usercardno": '6225561011937908',
            "ordertype": "10",
            'cvn2': '288',
            'expdate': '1904',
            "username": '潘豪',
            "userpid": '362201198602190014',
            "usertel": '13418071372',
            "backurl": "https://api.3035pay.com/testnotify",
            "returnurl": "https://api.3035pay.com/testnotify",

        }
    elif channel == 'dc':
        url = 'https://api.3035pay.com/pay/unionpay/quick/debit'
        data_obj = {
            "ordernumber": "jn" + orderdate,
            "amount": "400000",
            "orderdate": orderdate,
            "usercardno": '6228482309027715179',
            "ordertype": "10",
            "username": '陈贤丽',
            "userpid": '362201198602190014',
            "usertel": '13418071372',
            "backurl": "https://api.3035pay.com/testnotify",
            "returnurl": "https://api.3035pay.com/testnotify",

        }
    elif channel == 'sms':
        url = 'https://api.3035pay.com/pay/unionpay/quick/sms'
        data_obj = {
            # "ordernumber": "wk" + orderdate,
            "payorderno": "QPC201801241607490000003982551",
            "smscode": "975426"
        }
    elif channel == 'cs':
        data_obj = {

            "ordernumber": "jn" + orderdate,
            "amount": "224905",
            # "ordertype": "10",
            "orderdate": orderdate,
            "usercardno": "6214911200610399",
            "username": "许惠萍",
            "usertel": "18988972221",
            "userpid": "440724197809171449",
            "backurl": "https://gw.3035pay.com/testnotify",
            "returnurl": "https://gw.3035pay.com/testnotify",
        }
        url = 'https://gw.3035pay.com/pay/unionpay/entrust/credit'
    # elif channel == 'ds':
    #     data_obj = {
    #         "ordernumber": "wk" + orderdate,
    #         "amount": "700",
    #         "ordertype": "10",
    #         "orderdate": orderdate,
    #         "usercardno": "6222023602098798689",
    #         "username": "潘豪",
    #         "usertel": "13418071372",
    #         "userpid": "362201198602190014",
    #         "backurl": "https://api.3035pay.com/testnotify",
    #         "returnurl": "https://api.3035pay.com/testnotify",
    #     }
    #     url = 'https://api.3035pay.com/pay/unionpay/entrust/debit'
    elif channel == 'ds2':
        data_obj = {
            "ordernumber": "wk" + orderdate,
            "amount": "1000",
            "ordertype": "20",
            "orderdate": orderdate,
            "usercardno": "6222023602098798689",
            "username": "潘豪",
            "usertel": "13418071372",
            "userpid": "362201198602190014",
            "backurl": "https://api.3035pay.com/testnotify",
            "returnurl": "https://api.3035pay.com/testnotify",
        }
        url = 'https://api.3035pay.com/pay/unionpay/entrust/debit'

    pay_core(url, data_obj)
